package cn.alpha.intell.rbtlib.wifi.dlalog;

import android.app.Dialog;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.alpha.intell.rbtlib.R;


public class EditNetworkDialog extends Dialog {
    private static final String SAVE_CHANGE = "cn.alpha.intell.change.ip.STATIC";
    private static final String SAVE_CHANGE_DHCP = "cn.alpha.intell.change.ip.DHCP";
    private static final String CHANGE_FAIL_EXTRE = "reason";

    public EditNetworkDialog(Context context) {
        super(context);
    }

    public EditNetworkDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private void applyCompat() {
        if (Build.VERSION.SDK_INT < 19) {
            return;
        }
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        applyCompat();
        super.onCreate(savedInstanceState);
    }

    public interface ChangeResult {
        public void changeSuccess(WifiConfiguration configuration);

        public void changeWifiPass(String wifiPass);
    }

    public static class Builder {

        private Context context;
        private EditText edt_password;
        private String password;//密码
        private EditText edt_ip;
        private String ip;//IP地址
        private EditText edt_gateway;
        private String gateway;//网关
        private EditText edt_length;
        private String length;//网络前缀长度
        private EditText edt_DNS_1;
        private String DNS_1;//DNS 1
        private EditText edt_DNS_2;
        private String DNS_2;//DNS 2
        private String cancelText;
        private String saveText;

        private TextView mTextViewSSID;



        public Builder setSsid(String ssid) {
            this.ssid = ssid;
            return this;
        }

        private String ssid;

        private ImageView mImageViewClose;
        private OnClickListener cancelClickListener;
        private OnClickListener saveClickListener;
        private View.OnFocusChangeListener onFocusChangeListener;
        private WifiConfiguration configuration;
        private ChangeResult changeResult;
        private boolean haveChange = false;

        private EditWiFiCallback mEditWiFiCallback;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setOnFocusListener(View.OnFocusChangeListener onFocusListener) {
            this.onFocusChangeListener = onFocusListener;
            return this;
        }

        /**
         * 设置WiFi密码
         *
         * @param password
         * @return
         */
        public Builder setWiFiPassword(String password) {
            this.password = password;
            return this;
        }

        /**
         * 设置WiFi密码
         *
         * @param password
         * @return
         */
        public Builder setWiFiPassword(int password) {
            this.password = (String) context.getText(password);
            return this;
        }

        public Builder setCallBack(EditWiFiCallback callBack){
            this.mEditWiFiCallback = callBack;
            return this;
        }

        /***
         * 设置IP地址
         *
         * @param ip
         * @return
         */
        public Builder setWiFiIp(String ip) {
            this.ip = ip;
            return this;
        }

        public Builder setChangeResult(ChangeResult changeResult) {
            this.changeResult = changeResult;
            return this;
        }

        public Builder setConfiguration(WifiConfiguration configuration) {
            this.configuration = configuration;
            return this;
        }

        /***
         * 设置IP地址
         *
         * @param ip
         * @return
         */
        public Builder setWiFiIp(int ip) {
            this.ip = (String) context.getText(ip);
            return this;
        }

        /**
         * 设置网关
         *
         * @param gateway
         * @return
         */
        public Builder setWiFiGateway(String gateway) {
            this.gateway = gateway;
            return this;
        }

        /**
         * 设置网关
         *
         * @param gateway
         * @return
         */
        public Builder setWiFiGateway(int gateway) {
            this.gateway = (String) context.getText(gateway);
            return this;
        }

        /**
         * 设置前缀长度
         *
         * @param length
         * @return
         */
        public Builder setWiFiLength(String length) {
            this.length = length;
            return this;
        }

        /**
         * 设置前缀长度
         *
         * @param length
         * @return
         */
        public Builder setWiFiLength(int length) {
            this.length = length + "";
            return this;
        }

        /**
         * 设置DNS_1
         *
         * @param DNS_1
         * @return
         */
        public Builder setWiFiDNS_1(String DNS_1) {
            this.DNS_1 = DNS_1;
            return this;
        }

        /**
         * 设置DNS_1
         *
         * @param DNS_1
         * @return
         */
        public Builder setWiFiDNS_1(int DNS_1) {
            this.DNS_1 = (String) context.getText(DNS_1);
            return this;
        }

        /**
         * 设置DNS_2
         *
         * @param DNS_2
         * @return
         */
        public Builder setWiFiDNS_2(String DNS_2) {
            this.DNS_2 = DNS_2;
            return this;
        }

        /**
         * 设置DNS_2
         *
         * @param DNS_2
         * @return
         */
        public Builder setWiFiDNS_2(int DNS_2) {
            this.DNS_2 = (String) context.getText(DNS_2);
            return this;
        }

        /**
         * Set the negative button String and it's listener
         *
         * @param cancelText
         * @param cancelClickListener
         * @return
         */
        public Builder setCancelText(String cancelText, OnClickListener cancelClickListener) {
            this.cancelText = cancelText;
            this.cancelClickListener = cancelClickListener;
            return this;
        }

        /**
         * Set the negative button resource and it's listener
         *
         * @param cancelText
         * @param cancelClickListener
         * @return
         */
        public Builder setCancelText(int cancelText, OnClickListener cancelClickListener) {
            this.cancelText = (String) context.getText(cancelText);
            this.cancelClickListener = cancelClickListener;
            return this;
        }

        /**
         * Set the positive button String and it's listener
         *
         * @param saveText
         * @param saveClickListener
         * @return
         */
        public Builder setSaveText(String saveText, OnClickListener saveClickListener) {
            this.saveText = saveText;
            this.saveClickListener = saveClickListener;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param saveText
         * @param saveClickListener
         * @return
         */
        public Builder setSaveText(int saveText, OnClickListener saveClickListener) {
            this.saveText = (String) context.getText(saveText);
            this.saveClickListener = saveClickListener;
            return this;
        }

        public EditNetworkDialog create() {

            final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final EditNetworkDialog dialog = new EditNetworkDialog(context, R.style.EditNetworkDialog);
            final View view = inflater.inflate(R.layout.dialog_edit_betwork2, null);
            view.findViewById(R.id.dialog_isEntry_editNetwork_title).requestFocus();
            dialog.addContentView(view, new LinearLayout.LayoutParams(894, 682));

            mTextViewSSID = (TextView) view.findViewById(R.id.dialog_isEntry_editNetwork_title);
            mImageViewClose = (ImageView) view.findViewById(R.id.iv_close);
            edt_password = (EditText) view.findViewById(R.id.dialog_editNetwork_password);
            edt_ip = (EditText) view.findViewById(R.id.dialog_editNetwork_ip);
            edt_gateway = (EditText) view.findViewById(R.id.dialog_editNetwork_gateway);
            edt_length = (EditText) view.findViewById(R.id.dialog_editNetwork_length);
            edt_DNS_1 = (EditText) view.findViewById(R.id.dialog_editNetwork_DNS_1);
            edt_DNS_2 = (EditText) view.findViewById(R.id.dialog_editNetwork_DNS_2);

            if(!TextUtils.isEmpty(ssid)){
                mTextViewSSID.setText(ssid);
            }

            if(!TextUtils.isEmpty(ip)){
                edt_ip.setText(ip);
            }

            if(!TextUtils.isEmpty(gateway)) {
                edt_gateway.setText(gateway);
            }

            if(!TextUtils.isEmpty(length)){

                edt_length.setText(length);
            }

            if(!TextUtils.isEmpty(DNS_1)){
               edt_DNS_1.setText(DNS_1);
            }

            if(!TextUtils.isEmpty(DNS_2)){

                edt_DNS_2.setText(DNS_2);
            }


            mImageViewClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            TextView networkTv = (TextView) view.findViewById(R.id.dialog_detail_complete);

            networkTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String pwd = edt_password.getText().toString();
                    String ipString = edt_ip.getText().toString();
                    String gatway = edt_gateway.getText().toString();
                    String mask = edt_length.getText().toString();
                    String dns1 = edt_DNS_1.getText().toString();
                    String dns2 = edt_DNS_2.getText().toString();

                    if(mEditWiFiCallback!=null){
                        mEditWiFiCallback.onsave(ssid,pwd,ipString,mask,gatway,dns1,dns2);
                    }
                    dialog.dismiss();
                }
            });
            return dialog;
        }

    }


    public interface  EditWiFiCallback{

        void onsave(String ssid,String pwd,String ipString,String mask,String gatway,String dns1,String dns2);

    }


}
